package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.ReserveEditChildVH;
import com.centaline.androidsalesblog.adapter.viewholder.ReserveEditParentVH;
import com.centaline.androidsalesblog.iml.ReserveItemClick;
import com.centaline.androidsalesblog.model.businissobject.ReservationParentBo;
import com.centaline.androidsalesblog.model.databaseobject.PostDo;
import com.centaline.androidsalesblog.utils.CentaDistanceUtil;
import com.centaline.androidsalesblog.utils.PriceUtil;
import com.centanet.framework.glide.GlideLoad;
import com.h6ah4i.android.widget.advrecyclerview.utils.AbstractExpandableItemAdapter;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 未约看
 * Created by guilin on 16/4/23.
 */
public class ReservationEditAdapter extends
        AbstractExpandableItemAdapter<ReserveEditParentVH, ReserveEditChildVH> {

    private final int onlineColor = Color.rgb(255, 124, 18);
    private final int offlineColor = Color.rgb(102, 102, 102);

    final DrawableRequestBuilder<String> requestBuilder;
    final ArrayList<ReservationParentBo> parentBos;
    final ReserveItemClick reserveItemClick;

    private LayoutInflater inflater;
    private int mode;//0:约看模式;1:编辑模式

    public ReservationEditAdapter(Context context, DrawableRequestBuilder<String> requestBuilder,
                                  ArrayList<ReservationParentBo> parentBos, ReserveItemClick reserveItemClick) {
        this.requestBuilder = requestBuilder;
        this.parentBos = parentBos;
        this.reserveItemClick = reserveItemClick;
        inflater = LayoutInflater.from(context);
        setHasStableIds(true);
    }

    public void setMode(int mode) {
        this.mode = mode;
        for (ReservationParentBo reservationParentBo : parentBos) {
            reservationParentBo.setCheck(false);
            reservationParentBo.notifyCheckList(false);
        }
        notifyDataSetChanged();
    }

    public int getMode() {
        return mode;
    }

    @Override
    public int getGroupCount() {
        return parentBos.size();
    }

    @Override
    public int getChildCount(int groupPosition) {
        return parentBos.get(groupPosition).getPostDoArrayList().size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return parentBos.get(groupPosition).getEstateCode().hashCode();
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return parentBos.get(groupPosition).getPostDoArrayList().get(childPosition).getPostId().hashCode();
    }

    @Override
    public ReserveEditParentVH onCreateGroupViewHolder(ViewGroup parent, int viewType) {
        return new ReserveEditParentVH(inflater.inflate(R.layout.item_reserve_edit_parent, parent, false));
    }

    @Override
    public ReserveEditChildVH onCreateChildViewHolder(ViewGroup parent, int viewType) {
        return new ReserveEditChildVH(inflater.inflate(R.layout.item_reserve_edit_child, parent, false));
    }

    @Override
    public void onBindGroupViewHolder(ReserveEditParentVH holder, final int groupPosition, int viewType) {
        final ReservationParentBo reservationParentBo = parentBos.get(groupPosition);
        holder.divider.setVisibility(groupPosition == 0 ? View.GONE : View.VISIBLE);
        holder.img_order.setImageResource(reservationParentBo.isCheck() ?
                R.drawable.ic_order_btn_sel : R.drawable.ic_order_btn_nor);
        holder.fl_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reserveItemClick.check(!reservationParentBo.isCheck(), groupPosition, -1);
            }
        });
        if (0 == reservationParentBo.getStatus()) {
            holder.atv_name.setText(reservationParentBo.getEstateName());
            holder.rl_bottom.setVisibility(View.VISIBLE);
            holder.rl_estate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    reserveItemClick.itemClick(groupPosition, -1);
                }
            });
        } else {
            holder.rl_bottom.setVisibility(View.GONE);
        }
    }

    @Override
    public void onBindChildViewHolder(ReserveEditChildVH holder, final int groupPosition, final int childPosition, int viewType) {
        final PostDo postDo = parentBos.get(groupPosition).getPostDoArrayList().get(childPosition);
        final boolean isCheck = parentBos.get(groupPosition).getCheckList().get(childPosition);
        holder.img_order.setImageResource(isCheck ?
                R.drawable.ic_order_btn_sel : R.drawable.ic_order_btn_nor);
        boolean online = postDo.isOnline();
        holder.atv_status.setVisibility(mode == 1 || online ? View.GONE : View.VISIBLE);
        holder.img_order.setVisibility(mode == 1 || online ? View.VISIBLE : View.GONE);
        holder.view_line.setVisibility(View.VISIBLE);

        if (mode == 1 || online) {
            holder.fl_order.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    reserveItemClick.check(!isCheck, groupPosition, childPosition);
                }
            });
        }
        if ("s".equalsIgnoreCase(postDo.getPostType())) {//二手房
            GlideLoad.load(new GlideLoad.Builder(requestBuilder, postDo.getThumbPath())
                    .placeHolder(R.drawable.ic_centa_square_logo)
                    .error(R.drawable.ic_centa_square_logo)
                    .openWifiSwitch()
                    .into(holder.img_logo));
            holder.atv_title.setText(postDo.getTitle());
            holder.atv_tag_fir.setText(String.format(Locale.CHINA, "%d室%d厅  %.2f平  %s",
                    postDo.getBedroomCount(),
                    postDo.getSittingRoomCount(),
                    postDo.getSize(),
                    postDo.getDirection()));
            holder.atv_tag_thi.setVisibility(View.GONE);
            switch (viewType) {
                case 1:
                    holder.atv_tag_sec.setText(CentaDistanceUtil.distanceOfPostAndMetro(postDo.getRailLineNames(),
                            postDo.getRailWayName(),
                            postDo.getRailWayDistance()));
                    break;
                default:
                    holder.atv_tag_sec.setText(postDo.getCnEstate());
                    break;
            }
            holder.atv_price.setText(PriceUtil.salePrice(postDo.getPrice()));
            holder.atv_price.setTextColor(online ? onlineColor : offlineColor);
            holder.customListTagView.setTags(postDo.getKeyWords(), online);

            if (online) {
                holder.rl_right.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        reserveItemClick.itemClick(groupPosition, childPosition);
                    }
                });
            }
            if (!online) {
                holder.atv_status.setText("停售");
                holder.view_line.setVisibility(View.GONE);
            }
        } else {//租房
            GlideLoad.load(new GlideLoad.Builder(requestBuilder, postDo.getThumbPath())
                    .placeHolder(R.drawable.ic_centa_square_logo)
                    .error(R.drawable.ic_centa_square_logo)
                    .openWifiSwitch()
                    .into(holder.img_logo));
            holder.atv_title.setText(postDo.getTitle());
            holder.atv_tag_fir.setText(String.format(Locale.CHINA, "%d室%d厅  %s",
                    postDo.getBedroomCount(),
                    postDo.getSittingRoomCount(),
                    postDo.getDirection()));
            holder.atv_tag_sec.setText(postDo.getFitMent());
            holder.atv_tag_thi.setText(String.format(Locale.CHINA, "%s  %s", postDo.getGscope(), postDo.getCnEstate()));
            holder.atv_tag_thi.setVisibility(View.VISIBLE);
            holder.customListTagView.setVisibility(View.GONE);
            holder.atv_price.setText(PriceUtil.rentPrice(postDo.getRental()));
            holder.atv_price.setTextColor(online ? onlineColor : offlineColor);

            if (online) {
                holder.rl_right.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        reserveItemClick.itemClick(groupPosition, childPosition);
                    }
                });
            }
            if (!online) {
                holder.atv_status.setText("已租");
                holder.view_line.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public boolean onCheckCanExpandOrCollapseGroup(ReserveEditParentVH holder, int groupPosition, int x, int y, boolean expand) {
        return expand;
    }
}
